<?php

class Error
{
	public static function clihandler($severity, $message, $file, $line) {
		CLI::colorEcho("Error", true, CLI::RED);
        CLI::NL(2);
        echo "File:" . $file;
        CLI::NL(1);
        echo "Line:" . $line;
        CLI::NL(2);
        echo "Message:" . $message;
        CLI::NL(3);
        exit;
	}
		
	public static function handler($severity, $message, $file=false, $line=false){
		ob_clean();
		header("content-type:text/html");
		
		if($severity>0) {
			$type = Config::get("Error",$severity);
		} else {
			$type = "Undefined";
		}
		
		$data['severity'] = $severity;
		$data['message'] = $message;
		$data['file'] = $file;
		$data['line'] = $line;
		$data['backtraces'] = array_slice(debug_backtrace(),1);
		
		Log::append("Error", "$file:$line - $message");
		
		View::display("error.script",$data);
		exit;
	}
	
	public static function show($title, $description=false, $die=true)
	{
		if(php_sapi_name() == 'cli') {
			CLI::NL(1);
			CLI::colorEcho($title, true, CLI::RED);
	        echo $description;
	        CLI::NL(2);
		} else {
			ob_clean();
			
			$data['title'] = $title;
			$data['body'] = $description;
			
			View::display("error.server",$data);
		}
	
		if($die)
		exit;
	}
	
	public static function show404()
	{
		header("HTTP/1.1 404 Not Found");
		self::show("404 Page Not Found", "The page you requested was not found.");
	}
}